﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using XFControls.XFPanels.XFPanelItems;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace XFControls.XFPanels.XFPanelModals
{
    public class XFModalBottom : IXFModal
    {
        public XFPanelBase Parent { get; set; }
        public XFModalState State { get; set; }
        public Bitmap Buffer { get; set; }
        public XFItemStyle Style { get; set; }
        public int CurHeight { get; set; }
        public string Text { get; set; }
        public event ModalEventHandler OnSuccessAction;

        private XFModalYesButton yesButton;
        private XFModalNoButton noButton;
        private string originalText;
        private int _height;

        public XFModalBottom()
        {
            yesButton = new XFModalYesButton();
            noButton = new XFModalNoButton();

            State = XFModalState.Closed;

            Style = new XFItemStyle()
            {
                TextFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular),
                TextColor = Color.Black,
                Padding = 25,
                PaddingLeft = 50,
                PaddingRight = 50,
                BoarderTopColor = Color.Black,
                DashStyleTop = DashStyle.Solid
            };
        }

        public int GetHeight()
        {
            if (_height > 0)
                return _height;

            if (string.IsNullOrEmpty(originalText))
                originalText = Text;

            int width = Screen.PrimaryScreen.WorkingArea.Width - Style.PaddingLeft - Style.PaddingRight;

            Text = XFControlUtils.GetSizedString(width, originalText, Style.TextFont);

            using (Bitmap bm = new Bitmap(100, 100))
            {
                Graphics g = Graphics.FromImage(bm);

                var mes = g.MeasureString(Text, Style.TextFont);
                _height = (int)mes.Height + Style.PaddingTop + (Style.PaddingBottom * 2) + yesButton.GetHeight();
            }

            return GetHeight();
        }

        public void ResetHeight()
        {
            _height = 0;
        }

        public void ItemPaint(Graphics g, int x, int y)
        {
            XFControlUtils.DrawBoarders(Style, g, x, y, Screen.PrimaryScreen.WorkingArea.Width, _height);

            using (SolidBrush b = new SolidBrush(Style.TextColor))
                g.DrawString(Text, Style.TextFont, b, x + Style.PaddingLeft, y + Style.PaddingTop);

            int buttonPadding = (Screen.PrimaryScreen.WorkingArea.Width - (yesButton.GetWidth() + noButton.GetWidth())) / 3;
            int buttonStartY = GetHeight() - Style.PaddingBottom - yesButton.GetHeight();
            int currButtonX = buttonPadding;

            yesButton.ItemPaint(g, currButtonX, buttonStartY);
            currButtonX += yesButton.GetWidth() + buttonPadding;

            noButton.ItemPaint(g, currButtonX, buttonStartY);
            currButtonX += noButton.GetWidth() + Style.PaddingLeft;
        }

        public XFModalClickResult GetClickResult()
        {
            XFModalClickResult ret = XFModalClickResult.Cancel;

            if (noButton.Selected)
                return noButton.GetClickResult();

            else if (yesButton.Selected)
            {
                ret = yesButton.GetClickResult();

                if (OnSuccessAction != null)
                {
                    OnSuccessAction(this, new ModalEventArgs(ret));
                }
            }
            return ret;
        }

        public void SendClick(int x, int y)
        {
            int buttonPadding = (Screen.PrimaryScreen.WorkingArea.Width - (yesButton.GetWidth() + noButton.GetWidth())) / 3;
            int buttonStartY = GetHeight() - Style.PaddingBottom - yesButton.GetHeight();
            int currButtonX = buttonPadding;

            if (y > buttonStartY && y < buttonStartY + noButton.GetHeight())
            {
                if (x > currButtonX && x < currButtonX + yesButton.GetWidth())
                    yesButton.Selected = true;

                currButtonX += yesButton.GetWidth() + buttonPadding;
                
                if (x > currButtonX && x < currButtonX + noButton.GetWidth())
                    noButton.Selected = true;
            }
        }

        public void ClearClick()
        {
            yesButton.Selected = false;
            noButton.Selected = false;
        }

        public void Dispose()
        {
            if (Buffer != null)
                Buffer.Dispose();
        }
    }
}
